import {Component, Input, OnInit} from '@angular/core';
import {MemShippingAddress, MemShippingAddressCondition} from '../../mem-shipping-address.type';
import {ActivatedRoute, Router} from '@angular/router';
import {Services} from '../../../../../frame/module/services';
import {ErrRes} from '../../../../../frame/global/err-res';
import {isNullOrUndefined} from 'util';
import {UrlRou} from '../../../../../frame';
import {ModifyMemShippingAddressComponent} from '../modify-mem-shipping-address.component';

@Component({
    selector: 'app-main-modify-mem-shipping-address',
    templateUrl: './main-modify-mem-shipping-address.component.html',
    styleUrls: ['./main-modify-mem-shipping-address.component.css']
})
export class MainModifyMemShippingAddressComponent implements OnInit {

    errRes = new ErrRes();

    domain: MemShippingAddress = new MemShippingAddress();

    extend = {
        domain: {},
    };

    func = {
        loadDomain: (uuid: string) => {
            const condition: MemShippingAddressCondition = new MemShippingAddressCondition();
            condition.uuid = uuid;
            Services.memShippingAddress.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                }
            });
        },
        update: {
            req: (event) => {
                Services.memShippingAddress.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.memShippingAddress.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public modifyMemShippingAddress: ModifyMemShippingAddressComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.modifyMemShippingAddress.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
    }

}
